<?php

/**
 * CuentasCorrientes form.
 *
 * @package    tercermilenio
 * @subpackage form
 * @author     Your name here
 */
class CuentasCorrientesForm extends BaseCuentasCorrientesForm {

    public function configure() {
        $this->widgetSchema['cli_credito']->setAttribute('disabled', 'disabled');
        $this->widgetSchema['cta_cte_deuda']->setAttribute('disabled', 'disabled');

        if (!$this->isNew()) {
            $cta_cte = $this->getObject();
            $cliente = ClientesPeer::retrieveByPK($cta_cte->getCtaCteCliente());
            if (!empty($cliente)) {
                $credito = $cliente->getCliCredito();
                $this->setDefault('cli_credito', $credito);
            }
        }
    }

    public function setup() {
        parent::setup();
        $this->setWidgets(array(
            'cta_cte_cliente' => new sfWidgetFormInputHidden(),
            'cli_credito' => new sfWidgetFormInput(),
            'cta_cte_fecha_limite' => new sfWidgetFormJQueryDate(array('format' => '%day%/%month%/%year%')),
            'timestamp_fecha_limite' => new sfWidgetFormInputHidden(),
            'cta_cte_deuda' => new sfWidgetFormInput(),
            'deuda_hidden' => new sfWidgetFormInputHidden(),
            'cta_cte_pago' => new sfWidgetFormInput(),
        ));

        $this->setValidators(array(
            'cta_cte_cliente' => new sfValidatorPropelChoice(array('model' => 'Clientes', 'column' => 'cli_id', 'required' => false)),
            'cli_credito' => new sfValidatorNumber(array('required' => false)),
            'cta_cte_fecha_limite' => new sfValidatorDate(array('required' => false)),
            'cta_cte_deuda' => new sfValidatorNumber(array('required' => false)),
            'deuda_hidden' => new sfValidatorNumber(array('required' => false)),
            'timestamp_fecha_limite' => new sfValidatorNumber(array('required' => false)),
            'cta_cte_pago' => new sfValidatorNumber(array('required' => false)),
        ));

        $this->validatorSchema->setPreValidator(new sfValidatorAnd(array(
                    new sfValidatorSchemaCompare('cta_cte_fecha_limite', sfValidatorSchemaCompare::GREATER_THAN, gmdate('d/m/Y'),
                            array('throw_global_error' => true),
                            array('invalid' => 'La fecha límite debe ser posterior o igual a hoy.')
                    ),
                    new sfValidatorSchemaCompare('cta_cte_pago', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'deuda_hidden',
                            array('throw_global_error' => true),
                            array('invalid' => 'El precio a pagar debe ser menor o igual a la deuda.')
                    )//,
                   // new sfValidatorSchemaCompare('cli_credito', sfValidatorSchemaCompare::GREATER_THAN_EQUAL, 'cta_cte_deuda',
                   //         array('throw_global_error' => true),
                   //         array('invalid' => 'El credito debe ser mayor o igual a la deuda')
                   // )
                )));


        $this->widgetSchema->setNameFormat('cuentas_corrientes[%s]');
        $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
    }

}
